package com.deram.ctin.rest.web.controller.common;

import com.deram.ctin.common.utils.BusinessUtils;
import com.deram.ctin.common.utils.DateUtils;
import com.deram.ctin.common.utils.PasswordUtils;
import com.deram.ctin.common.utils.ValidateUtils;
import com.deram.ctin.common.vo.UserVo;
import com.deram.ctin.dependencies.rest.base.BaseController;
import com.deram.ctin.rest.web.model.common.CommonUserRegisterParamModel;
import com.deram.ctin.serv.member.api.MemberInfoApi;
import com.deram.ctin.serv.member.entity.MemberInfo;
import com.deram.ctin.serv.system.api.SystemUserApi;
import com.deram.ctin.serv.system.entity.SystemUser;
import com.deram.ctin.serv.third.api.CaptchaApi;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * Created by xhw on 2017/09/12.
 */

@Api(tags = {"common - 用户接口"})
@RestController
@RequestMapping("/common/user")
public class CommonUserController extends BaseController {


    @Autowired
    private SystemUserApi systemUserApi;

    @Autowired
    private MemberInfoApi memberInfoApi;



    @Autowired
    private CaptchaApi captchaApi;

    @ApiOperation(value = "登陆")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "username", value = "用户名", required = true, paramType = "form"),
            @ApiImplicitParam(name = "password", value = "密码", required = true, paramType = "form")
    })
    @PostMapping("/login")
    public void login(@RequestParam String username, @RequestParam String password) {
        ValidateUtils.notEmpty(username, "用户名");
        ValidateUtils.notEmpty(password, "密码");
        SystemUser user = systemUserApi.findByUsername(username);
        BusinessUtils.notNull(user, "用户名或密码不正确");
        if (!PasswordUtils.isEqual(user.getPassword(), password, user.getSalt())) {
            BusinessUtils.throwException("用户名或密码不正确");
        }
        UserVo userVo = new UserVo();
        userVo.setId(user.getId());
        userVo.setName(user.getUsername());
        super.create(userVo);
    }

    @ApiOperation(value = "手机号验证码登陆")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "mobile", value = "手机号", required = true, paramType = "form"),
            @ApiImplicitParam(name = "captcha", value = "手机验证码", required = true, paramType = "form")
    })
    @PostMapping("/loginByCaptcha")
    public void loginByCaptcha(@RequestParam String mobile, @RequestParam String captcha) {
        ValidateUtils.notEmpty(mobile, "手机号");
        ValidateUtils.notEmpty(captcha, "验证码");

        captchaApi.verify(mobile, captcha);

        SystemUser user = systemUserApi.findByUsername(mobile);
        if (null == user) {
            SystemUser userParam = new SystemUser();
            userParam.setUsername(mobile);
            user = systemUserApi.create(userParam);
            MemberInfo memberInfo = new MemberInfo();
            memberInfo.setUserId(user.getId());
            memberInfo.setCreateDate(DateUtils.getNow());
            memberInfoApi.save(memberInfo);
        }
        UserVo userVo = new UserVo();
        userVo.setId(user.getId());
        userVo.setName(user.getUsername());
        super.create(userVo);
    }

    @ApiOperation(value = "注销")
    @PostMapping("/logout")
    public void logout() {
        super.clear();
    }

    @ApiOperation(value = "注册")
    @PostMapping("/register")
    public void register(@RequestBody CommonUserRegisterParamModel param) {
        ValidateUtils.isMobile(param.getMobile());
        ValidateUtils.isPassword(param.getPassword(), "密码");
        ValidateUtils.notEmpty(param.getCaptcha(), "验证码");

        SystemUser userParam = new SystemUser();
        userParam.setUsername(param.getMobile());
        userParam.setPassword(param.getPassword());
        SystemUser user = systemUserApi.create(userParam);
        MemberInfo memberInfo = new MemberInfo();
        memberInfo.setUserId(user.getId());
        memberInfo.setCreateDate(DateUtils.getNow());
        memberInfoApi.save(memberInfo);
        UserVo userVo = new UserVo();
        userVo.setId(user.getId());
        userVo.setName(user.getUsername());
        super.create(userVo);
    }

    @ApiOperation(value = "忘记密码")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "mobile", value = "手机号", required = true, paramType = "form"),
            @ApiImplicitParam(name = "newPassword", value = "新密码", required = true, paramType = "form"),
            @ApiImplicitParam(name = "captcha", value = "验证码", required = true, paramType = "form"),
    })
    @PostMapping("/forgetPassword")
    public void forgetPassword(String mobile, String newPassword, String captcha) {
        ValidateUtils.isMobile(mobile);
        ValidateUtils.isPassword(newPassword, "新密码");
        ValidateUtils.notEmpty(captcha, "验证码");

        captchaApi.verify(mobile, captcha);

        SystemUser user = systemUserApi.findByUsername(mobile);
        BusinessUtils.notNull(user, "用户不存在");
        systemUserApi.updatePassword(user.getId(), newPassword);
    }






}
